<template>
  <div class="app-container">
<!--搜索框-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="质检员用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入质检员用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="质检员姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入质检员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="质检员状态" clearable>
          <el-option
            v-for="dict in dict.type.lg_quality_man_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--列表展示-->
    <el-table v-loading="loading" :data="postList" >

      <el-table-column label="质检员编号" align="center" prop="id" />
      <el-table-column label="质检员用户名" align="center" prop="username" />
      <el-table-column label="质检员姓名" align="center" prop="realName" />
      <el-table-column label="用户头像" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_quality_man_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-link"
            @click="bindQualityMan(scope.row)"
          >绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改弹出的质检员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="质检员头像" prop="avatar">
          <image-upload v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="质检员名字" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入质检员名字" />
        </el-form-item>
        <el-form-item label="质检员用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="质检员账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入质检员密码" />
        </el-form-item>
        <el-form-item label="质检员密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入质检员密码" />
        </el-form-item>
        <el-form-item label="身份证正面照" prop="frontImage">
          <image-upload v-model="form.frontImage"/>
        </el-form-item>
        <el-form-item label="身份证反面照" prop="backImage">
          <image-upload v-model="form.backImage"/>
        </el-form-item>

        <el-form-item label="质检员手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入质检员手机号" />
        </el-form-item>

        <el-form-item label="质检员状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.lg_quality_man_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQualityMan, getQualityMan, } from "@/api/quality/qualityMan";
import {bindQualityMan } from "@/api/quality/goQuality";

export default {
  name: "Post",
  dicts: ['lg_quality_man_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质检员表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: undefined,
        realName: undefined,
        status: undefined,
        phone: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "质检员名称不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "质检员用户名不能为空", trigger: "blur" }
        ],
        account: [
          { required: true, message: "质检员密码不能为空", trigger: "blur" }
        ]
      },
      // 防抖定时器
      debounceTimer: null
    }
  },
  props: {
    orderNumber: {
      type: Number,
      required: true
    }

  },
  created() {
    // this.currentOrderId = this.$route.query.orderId || null;
    console.log("接收到的订单号:", this.orderNumber);
    this.getList();
  },
  beforeDestroy() {
    // 组件销毁前清除定时器
    if (this.debounceTimer) {
      clearTimeout(this.debounceTimer);
    }
  },
  methods: {
    /** 绑定质检员到订单 */
    bindQualityMan(row) {
      // 防止重复点击
      if (this.loading) return;
      
      this.$modal.confirm(`确定要将该质检员（${row.realName}）指派给当前订单吗？`).then(() => {
        this.loading = true;
        // 调用后端 API 绑定质检员
        return bindQualityMan({
          orderNumber: this.orderNumber,  // 当前订单 ID
          inspectorId: row.id,  // 质检员 ID
        });
      }).then(() => {
        this.$modal.msgSuccess("绑定成功");
        this.$emit('close'); // 关闭弹窗
      }).catch(() => {}).finally(() => {
        this.loading = false;
      });
    },
    /** 查询质检员列表 */
    getList() {
      // 防抖处理，避免频繁请求
      if (this.debounceTimer) {
        clearTimeout(this.debounceTimer);
      }
      
      this.debounceTimer = setTimeout(() => {
        this.loading = true;
        listQualityMan(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
        }).catch(error => {
          console.error("获取质检员列表失败:", error);
        }).finally(() => {
          this.loading = false;
        });
      }, 200);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        username: undefined,
        realName: undefined,
        account: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
